//
//  MBProgressHUD+NJ.m
//  NJWisdomCard
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 Weconex. All rights reserved.
//

#import "MBProgressHUD+NJ.h"


@implementation MBProgressHUD (NJ)


+ (MBProgressHUD *)showText:(NSString *)text {
    MBProgressHUD *hud = [MBProgressHUD showText:text toView:nil];
    return hud;
}

+ (MBProgressHUD *)showText:(NSString *)text toView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (text == nil || [text isKindOfClass:[NSNull class]]) {
        text = @"";
    }
    hud.label.text = text;
    hud.label.font = [UIFont systemFontOfSize:KWidth(16.0f)];
    hud.label.numberOfLines = 0;
    hud.minSize = CGSizeMake(KWidth(80), KHeight(40));
    
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    hud.contentColor = [UIColor whiteColor];
    hud.mode = MBProgressHUDModeText;
    
    hud.animationType = MBProgressHUDAnimationZoomOut;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5];
    return hud;
}


/**
 *  显示信息
 *
 *  @param text 信息内容
 *  @param icon 图标
 *  @param view 显示的视图
 */
+ (MBProgressHUD *)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (text == nil || [text isKindOfClass:[NSNull class]]) {
        text = @"";
    }
    hud.label.text = text;
    hud.label.font = [UIFont systemFontOfSize:KWidth(16.0f)];
    hud.label.numberOfLines = 0;
    
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    hud.contentColor = [UIColor whiteColor];
    
    if ([Helper widthOfString:text font:hud.label.font height:0] > KWidth(100)) {
        hud.mode = MBProgressHUDModeText;
    } else {
        // 设置图片
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
        // 再设置模式
        hud.mode = MBProgressHUDModeCustomView;
    }
    
    hud.animationType = MBProgressHUDAnimationZoomOut;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5];
    return hud;
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    显示信息的视图
 */
+ (MBProgressHUD *)showSuccess:(NSString *)success toView:(UIView *)view
{
    return [self show:success icon:@"success.png" view:view];
}

/**
 *  显示错误信息
 *
 */
+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (MBProgressHUD *)showError:(NSString *)error toView:(UIView *)view{
    return [self show:error icon:@"error.png" view:view];
}

/**
 *  显示错误信息
 *
 *  @param message 信息内容
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}

/**
 *  显示一些信息
 *
 *  @param message 信息内容
 *  @param view    需要显示信息的视图
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (message == nil || [message isKindOfClass:[NSNull class]]) {
        message = @"";
    }
    hud.label.text = message;
    hud.label.font = [UIFont systemFontOfSize:KWidth(16.0f)];
    hud.contentColor = [UIColor whiteColor];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.backgroundColor = rgba(0, 0, 0, 0.7);
    
    // YES代表需要蒙版效果
    hud.backgroundView.backgroundColor = [UIColor clearColor];
    return hud;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    [self hideHUDForView:view animated:YES];
}

@end
